﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XsqTech.SmartOA.Infrastructure.Filter
{
    public class GlobalExceptionFilter : IExceptionFilter
    {
        private readonly ILogger<GlobalExceptionFilter> _logger;

        public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger)
        {
            _logger = logger;
        }

        public void OnException(ExceptionContext context)
        {

            Exception ex = context.Exception;

            var message = "错误信息为" + ex.Message;

            _logger.LogError(message); //写入日志

            context.ExceptionHandled = true; //把异常标记为已处理
        }
    }
}
